/*
 * mapAtlas.java
 *
 * Copyright (C) 2008 Paul Nettleship
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */
package staticTrailExplorer;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
 *
 * @author codebauer
 */
public class mapAtlas implements mapDataIF
   {
   private ZipFile inputZipPhile = null;
   private ZipOutputStream outputZipStream = null;
   private tileMap map;

   public void init(tileMap map)
      {
      this.map = map;
      map.calibrated = true;
      }

   public void openInput(File phile) throws IOException, ZipException
      {
      inputZipPhile = new ZipFile(phile);
      }

   public void openOutput(File phile) throws FileNotFoundException
      {
      outputZipStream = new ZipOutputStream(new FileOutputStream(phile));
      }

   public void close() throws IOException
      {
      if( outputZipStream != null )
         outputZipStream.close();
      if( inputZipPhile != null )
         inputZipPhile.close();
      }

   public location getDefaultStartLocation()
      {
      return new location(0,0,location.maxZoom);
      }

   public BufferedImage loadMapTile(location loc)
      {
      BufferedImage tile = null;
      if (inputZipPhile != null)
         {
         synchronized (this)
            {
            ZipEntry zipEntry;
            try
               {
               zipEntry = inputZipPhile.getEntry(loc.getName(map));
               if (zipEntry != null)
                  {
                  tile = ImageIO.read(inputZipPhile.getInputStream(zipEntry));
                  }
               }
            catch (IOException ex)
               {
               System.out.print(ex);
               }
            }
         }
      return tile;
      }

   public void saveMapTile(BufferedImage tile, location loc) throws IOException
      {
      if (tile != null)
         {
         outputZipStream.putNextEntry(new ZipEntry(loc.getName(map)));
         ImageIO.write(tile, "png", outputZipStream);
         outputZipStream.closeEntry();
         }
      }

   public void setCalibration()
      {
      String text = "Can not calibrate. " + map.name + " already calibarated.\n";
      text += "Try selecting another window!";
      JOptionPane.showMessageDialog(null,text,"Warning",JOptionPane.PLAIN_MESSAGE);
      }
   }
